package com.xierong.remoteplayerlauncher.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import com.xierong.remoteplayerlauncher.utils.Def;
import com.xierong.remoteplayerlauncher.utils.PlayerManager;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class UDPListenerService extends Service {
    public static final String UDP_RECEIVER = "com.xierong.udp.receiver";
    public DatagramSocket socket = null;
    @Override
    public void onCreate() {
        super.onCreate();
        new Thread(new UDPClient()).start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        Log.d(PlayerManager.TAG, "UDP监听服务已经被销毁");
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(PlayerManager.TAG, "UDP服务：onBind");
        return null;
    }
    public class UDPClient implements Runnable {
        @Override
        public void run() {
            try {
                if (socket == null) {
                    socket = new DatagramSocket(3321);
                    socket.setReuseAddress(true);
                }
                Log.d(PlayerManager.TAG, "UDP监听已经开启");
                while(true) {
                    byte[] buffer = new byte[6];
                    DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
                    socket.receive(packet);
                    String command = PlayerManager.bytes2HexString(packet.getData());
                    Log.d(PlayerManager.TAG, "接收到的信息：" + command);
                    switch (command) {
                        case Def.PLAY: {
                            Intent intent = new Intent(UDP_RECEIVER);
                            intent.putExtra("action", Def.PLAY);
                            sendBroadcast(intent);
                        };break;
                        case Def.PAUSE: {
                            Intent intent = new Intent(UDP_RECEIVER);
                            intent.putExtra("action", Def.PAUSE);
                            sendBroadcast(intent);
                        };break;
                        case Def.RESTART: {
                            Intent intent = new Intent(UDP_RECEIVER);
                            intent.putExtra("action", Def.RESTART);
                            sendBroadcast(intent);
                        };break;
                        case Def.LOOP: {
                            Intent intent = new Intent(UDP_RECEIVER);
                            intent.putExtra("action", Def.LOOP);
                            sendBroadcast(intent);
                        };break;
                        case Def.VOL_UP: {
                            Intent intent = new Intent(UDP_RECEIVER);
                            intent.putExtra("action", Def.VOL_UP);
                            sendBroadcast(intent);
                        };break;
                        case Def.VOL_DOWN: {
                            Intent intent = new Intent(UDP_RECEIVER);
                            intent.putExtra("action", Def.VOL_DOWN);
                            sendBroadcast(intent);
                        };break;
                        case Def.PREV: {
                            Intent intent = new Intent(UDP_RECEIVER);
                            intent.putExtra("action", Def.PREV);
                            sendBroadcast(intent);
                        };break;
                        case Def.NEXT: {
                            Intent intent = new Intent(UDP_RECEIVER);
                            intent.putExtra("action", Def.NEXT);
                            sendBroadcast(intent);
                        };break;
                        default: {
                            Log.d(PlayerManager.TAG, "接收到未知UDP消息：" + command);
                        }
                    }
                }
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
